import bridge from '@king-fisher/bridge';

export function getNativeSelfCodeList() {
  return new Promise(resolve => {
    const param = { action: 'getAllStocks' };
    bridge.callNativeHandlerAsync('selfCodeAction', [param] as any, (res: any) => {
      if (typeof res.data === 'string') {
        const data = JSON.parse(res.data);
        resolve(data);
      } else {
        resolve(res.data);
      }
    });
  });
}

export function addCodeIntoList(stockcode: string, marketid: string) {
  return new Promise(resolve => {
    const param = {
      action: 'addStocks',
      selfCodeListAdd: [{ stockcode: stockcode, marketid: marketid }],
    };
    bridge.callNativeHandlerAsync('selfCodeAction', [param] as any, res => {
      if (typeof res === 'string') {
        const data = JSON.parse(res);
        resolve(data);
      } else {
        resolve(res);
      }
    });
  });
}

export function removeCodeFromList(stockcode: string, marketid: string) {
  return new Promise(resolve => {
    const param = {
      action: 'delStocks',
      selfCodeListDel: [{ stockcode: stockcode, marketid: marketid }],
    };
    bridge.callNativeHandlerAsync('selfCodeAction', [param] as any, res => {
      if (typeof res === 'string') {
        const data = JSON.parse(res);
        resolve(data);
      } else {
        resolve(res);
      }
    });
  });
}
